<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\GoodsService;
use App\Service\Admin\ImageService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class GoodsController extends BaseController
{
    public function getCategories()
    {
        $categories = DB::table('goods_cate')
            ->where('del', 0) // 未删除的分类
            ->orderBy('id', 'asc')
            ->get(['id', 'title']); // 只取 id 和 title 字段

        if ($categories->isEmpty()) {
            return $this->toJson(-1, '没有分类数据');
        }

        return $this->toJson(0, 'success', $categories);
    }
    //列表
    public function getIndex(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);

        $pas['gno'] = $request->post('gno', '');
        $pas['title'] = $request->post('title', '');
        $pas['cate_id'] = $request->post('cate_id', '');
        $pas['sale_min'] = $request->post('sale_min', '');
        $pas['sale_max'] = $request->post('sale_max', '');
        $pas['is_shelf'] = $request->post('is_shelf', '');
        $pas['isnew'] = $request->post('isnew', '');


        $res = (new GoodsService())->getList($pages,$pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }
    //批量 上下架
    public function batchStatus(Request $request)
    {
        $ids=$request->post('ids',[]);
        $status=$request->post('status',1);
        $res= (new GoodsService())->batchStatus($ids,$status);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '修改成功');
    }
    //批量 删除
    public function batchDel(Request $request)
    {
        $ids=$request->post('ids',[]);
        $res= (new GoodsService())->batchDel($ids);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '刪除成功');
    }
    //分类
    public function getCateList()
    {
        $res = (new GoodsService())->getCateList();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //一个
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $res = (new GoodsService())->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //添加
    public function addOne(Request $request)
    {
//        $this->validate($request, [
//            'title' => 'required|min:1|max:255',
//            'cateid' => 'required',
//            'bigImg' => 'required',
//            'moreImgs' => 'required',
//            'skulist' => 'required',
//        ], [
//            'title.required' => '商品名称必须的',
//            'cateid.required' => '商品分类必须的',
//            'moreImgs.required' => '详情图必须的',
//            'skulist.required' => 'SKU规格必须的',
//        ]);
        $pas['title'] = $request->post('title', '');
        $pas['cate_id'] = $request->post('cateid', 0);
        $pas['bigimg'] = $request->post('bigImg', '');
        $pas['isnew'] = $request->post('isnew', 0);
        $pas['detail'] = $request->post('detail', '');
        $cheaps = $request->post('cheaps', '');
        $moreImgs = $request->post('moreImgs', []);
        $skulist = $request->post('skulist', []);
        if (!$pas['title']) {
            return $this->toJson(-2, '商品名称必须的');
        }
        if (!$pas['cate_id']) {
            return $this->toJson(-2, '商品分类必须的');
        }
        if (!$pas['bigimg']) {
            return $this->toJson(-2, '主图必须的');
        }
        if (!count($moreImgs)) {
            return $this->toJson(-2, '详情图必须的');
        }
        if (!count($skulist)) {
            return $this->toJson(-2, 'SKU规格必须的');
        }
//        $img = Upload::getPathByBase64($pas['bigimg']);
//        $imgInfo = getimagesize($pas['bigimg']);
//        $pas['bigimg'] = $img;
//        (new ImageService())->addOne($img, $imgInfo[0], $imgInfo[1]);
        if ($cheaps['per'] && !$cheaps['start_time']) {
            return $this->toJson(-2, '折扣开始时间必须的');
        }
        if ($cheaps['per'] && !$cheaps['end_time']) {
            return $this->toJson(-2, '折扣结束时间必须的');
        }
        $res = (new GoodsService())->addOne($pas, $moreImgs, $skulist, $cheaps);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //详情图上传
    public function moreUpload(Request $request)
    {
//        $this->validate($request, [
//            'id' => 'required',
//        ], [
//            'id.required' => '商品名称必须的',
//        ]);
        $id = $request->post('id', 0);
        $img = $request->post('img', '');
        if (!$id) {
            return $this->toJson(-2, '商品ID必须的');
        }
        if (!$img) {
            return $this->toJson(-2, '商品图片必须的');
        }
        $moreImg = Upload::getPathByBase64($img);
        $imgInfo = getimagesize($img);
        $res = (new GoodsService())->moreUpload($id, $moreImg, $imgInfo[0], $imgInfo[1]);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //详情图删除
    public function moreDel(Request $request)
    {
//        $this->validate($request, [
//            'id' => 'required',
//        ], [
//            'id.required' => '商品ID必须的',
//        ]);
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '商品ID必须的');
        }
        $res = (new GoodsService())->moreDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //修改
    public function modify(Request $request)
    {
//        $this->validate($request, [
//            'id' => 'required',
//            'title' => 'required|min:1|max:255',
//            'cateid' => 'required',
//            'bigImg' => 'required',
//        ], [
//            'id.required' => '商品ID必须的',
//            'title.required' => '商品名称必须的',
//            'cateid.required' => '商品分类必须的',
//        ]);
        $id = $request->post('id', 0);
        $pas['title'] = $request->post('title', '');
        $pas['cate_id'] = $request->post('cateid', 0);
        $pas['bigimg'] = $request->post('bigImg', '');
        $pas['isnew'] = $request->post('isnew', 0);
        $pas['detail'] = $request->post('detail', '');
        $cheaps = $request->post('cheaps', '');
        if (!$id) {
            return $this->toJson(-2, '商品ID必须的');
        }
        if (!$pas['title']) {
            return $this->toJson(-2, '商品名称必须的');
        }
        if (!$pas['cate_id']) {
            return $this->toJson(-2, '商品分类必须的');
        }
        $res = (new GoodsService())->modify($id, $pas, $cheaps);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function setDel(Request $request)
    {
//        $this->validate($request, [
//            'id' => 'required',
//        ], [
//            'id.required' => '商品ID必须的',
//        ]);
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '商品ID必须的');
        }
        $res = (new GoodsService())->setDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //SKU添加
    public function skuAdd(Request $request)
    {
//        $this->validate($request, [
//            'gid' => 'required',
//            'title' => 'required',
//            'price' => 'required',
//            'price_line' => 'required',
//            'stock' => 'required',
//        ], [
//            'gid.required' => '商品ID必须的',
//            'title.required' => '规格名称必须的',
//            'price.required' => '单价必须的',
//            'price_line.required' => '划线价必须的',
//            'stock.required' => '库存必须的',
//        ]);
        $pas['gid'] = $request->post('gid', 0);
        $pas['title'] = $request->post('title', '');
        $pas['price'] = $request->post('price', 0);
        $pas['price_line'] = $request->post('price_line', 0);
        $pas['stock'] = $request->post('stock', 0);
        $pas['rate'] = $request->post('rate', 0);
        if (!$pas['gid']) {
            return $this->toJson(-2, '商品ID必须的');
        }
        if (!$pas['title']) {
            return $this->toJson(-2, '规格名称必须的');
        }
        if (!$pas['price']) {
            return $this->toJson(-2, '单价必须的');
        }
        if (!$pas['price_line']) {
            return $this->toJson(-2, '划线价必须的');
        }
//        if (!$pas['stock']) {
//            return $this->toJson(-2, '库存必须的');
//        }
        $res = (new GoodsService())->skuAdd($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //SKU修改
    public function skuModify(Request $request)
    {
//        $this->validate($request, [
//            'id' => 'required',
//            'title' => 'required',
//            'price' => 'required',
//            'price_line' => 'required',
//            'stock' => 'required',
//        ], [
//            'id.required' => 'SKU的ID必须的',
//            'title.required' => '规格名称必须的',
//            'price.required' => '单价必须的',
//            'price_line.required' => '划线价必须的',
//            'stock.required' => '库存必须的',
//        ]);
        $id = $request->post('id', 0);
        $pas['title'] = $request->post('title', '');
        $pas['price'] = $request->post('price', 0);
        $pas['price_line'] = $request->post('price_line', 0);
        $pas['stock'] = $request->post('stock', 0);
        $pas['rate'] = $request->post('rate', 0);
        if (!$id) {
            return $this->toJson(-2, 'SKU的ID必须的');
        }
        if (!$pas['title']) {
            return $this->toJson(-2, '规格名称必须的');
        }
        if (!$pas['price']) {
            return $this->toJson(-2, '单价必须的');
        }
        if (!$pas['price_line']) {
            return $this->toJson(-2, '划线价必须的');
        }
//        if (!$pas['stock']) {
//            return $this->toJson(-2, '库存必须的');
//        }
        $res = (new GoodsService())->skuModify($id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //SKU删除
    public function skuDel(Request $request)
    {
//        $this->validate($request, [
//            'id' => 'required',
//        ], [
//            'id.required' => 'SKU的ID必须的',
//        ]);
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, 'SKU的ID必须的');
        }
        $res = (new GoodsService())->skuDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //上下架
    public function setShelf(Request $request)
    {
        $id = $request->post('id', 0);
        $shelf = $request->post('shelf', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = (new GoodsService())->setShelf($id, $shelf);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //搜索词历史
    public function getWordList(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = (new GoodsService())->getWordList($pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //导出功能，已废弃
    public function getOut(Request $request)
    {
        $res =(new GoodsService())->getGlist();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $res = $res['data'];
        $titles = ['商品图片', '商品编号', '商品名称', '商品分类', '商品销量', '是否为上新'];
        $fname = 'daochu/scgoods-'.time().'.txt';
        file_put_contents($fname, implode('<-->', $titles).PHP_EOL, FILE_APPEND);
        foreach ($res as $v) {
            $arys = [
                $v['img'], $v['gno'], $v['title'], $v['cate_name'], $v['sale'], $v['isnew'],
            ];
            file_put_contents($fname, implode('<-->', $arys).PHP_EOL, FILE_APPEND);
        }
        $fpath = env('DOMAIN').'/'.$fname;
        return $this->toJson(0, '', $fpath);
    }

    //SKU列表
    public function skuList(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $gid = $request->post('gid', 0);
        $res = (new GoodsService())->skuList($pages, $gid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }
}